#include "EntryToolTip.h"
#include "Icons.h"
#include "../../core/Impp.h"

namespace Impp
{

EntryToolTip::EntryToolTip(wxWindow *parent) :
	ImppPopupWindow(parent),curBuddy(NULL),curPos(wxDefaultPosition)
{
	mainPane = new wxPanel(this);
	wxPanel* avatarAndExtInfo = new wxPanel(mainPane);

	wxBoxSizer* sizer = new wxBoxSizer(wxVERTICAL);
	wxBoxSizer* sizerAAE = new wxBoxSizer(wxHORIZONTAL);

	name = new wxStaticText(mainPane,wxID_ANY,wxT(""));


	iUserInterface* ui = Impp::exemplar()->getModuleManager()->getUserInterfaceModule();
	wxBitmap noAvatar;
	if(ui != NULL){
		noAvatar = ui->getIcons()->getSpecialBitmap(BITMAP_NO_AVATAR);
	}

	avatarBitmap = new wxStaticBitmap(avatarAndExtInfo,wxID_ANY,noAvatar);
	extendedInfo= new wxStaticText(avatarAndExtInfo,wxID_ANY,wxT(""));
	statusMsg = new wxStaticText(mainPane,wxID_ANY,wxT(""));

	wxFont f = name->GetFont();
	f.SetWeight(wxFONTWEIGHT_BOLD);
	name->SetFont(f);


	sizerAAE->Add(avatarBitmap,0,wxNORMAL,5);
	sizerAAE->Add(extendedInfo,0,wxNORMAL,5);

	sizer->Add(name,0,wxNORMAL);
	sizer->Add(avatarAndExtInfo,0,wxNORMAL);
	sizer->Add(statusMsg,0,wxNORMAL);

	avatarAndExtInfo->SetSizer(sizerAAE);
	mainPane->SetSizer(sizer);

	mainPane->SetSize(280,180);
	mainPane->SetPosition(wxPoint(10,10));
	SetSize(300,200);
	SetPosition(wxPoint(50,50));
	Layout();
}

EntryToolTip::~EntryToolTip()
{
}

void EntryToolTip::showBuddyInfo(Buddy* buddy,const wxPoint & pos){
	curBuddy = buddy;
	curPos = pos;

	name->SetLabel(buddy->getName());
	statusMsg->SetLabel(buddy->getStatusMsg());
	statusMsg->Wrap(280);

	wxString extInfo;

	wxString nickname = buddy->getExtendedInfo(BUDDY_EXT_NICKNAME);
	wxString firstname = buddy->getExtendedInfo(BUDDY_EXT_FIRSTNAME);
	wxString lastname = buddy->getExtendedInfo(BUDDY_EXT_LASTNAME);
	wxString email = buddy->getExtendedInfo(BUDDY_EXT_EMAIL);
	wxString lastSeenS = buddy->getExtendedInfo(BUDDY_EXT_LAST_SEEN);
	long lastSeenI = 0;
	wxDateTime lastSeen;
	if(!lastSeenS.IsEmpty()){
		lastSeenS.ToLong(&lastSeenI);
		lastSeen = wxDateTime((time_t) lastSeenI);
	}



	wxString onlineTime;
	wxString idleTimes;
	wxString ipAddress;

	wxDateTime time;
	wxTimeSpan span;
	wxDateTime idleTime;
	wxTimeSpan idleTimeSpan;
	if(buddy->getOnlineTime(time)){
		onlineTime << time.FormatTime();
		span = wxDateTime::Now()-time;
	}
	if(buddy->getIdleTime(idleTime)){
		idleTimes << idleTime.FormatTime();
		idleTimeSpan = wxDateTime::Now()-idleTime;
	}
	buddy->getIp(ipAddress);



	extInfo << (nickname.IsEmpty() ? wxT("") : wxString(wxT("Nickname: ")) << nickname << wxT("\n"));
	extInfo << (firstname.IsEmpty() ? wxT("") : wxString(wxT("Firstname: ")) << firstname << wxT("\n"));
	extInfo << (lastname.IsEmpty() ?wxT("") : wxString(wxT("Lastname: ")) << lastname << wxT("\n"));
	extInfo << (email.IsEmpty() ? wxT(""): wxString(wxT("Email: ")) << email << wxT("\n"));
	extInfo << (onlineTime.IsEmpty() ? wxT("") : wxString(wxT("Online since: ")) << onlineTime << wxT(" (")<< span.GetHours() << wxT("h ") << (span.GetMinutes()-60*span.GetHours()) << wxT("min)\n"));
	extInfo << (idleTimes.IsEmpty() ? wxT("") : wxString(wxT("Idle since: ")) << idleTimes << wxT(" (")<< idleTimeSpan.GetHours() << wxT("h ") << (idleTimeSpan.GetMinutes()-60*idleTimeSpan.GetHours()) << wxT("min)\n"));
	extInfo << (ipAddress.IsEmpty() ? wxT(""): wxString(wxT("Ip-Address: ")) << ipAddress << wxT("\n"));
	extInfo << wxT("Last seen: ") << ( lastSeenS.IsEmpty() ? wxT("never") : lastSeen.Format(wxT("%d-%m-%Y %H:%M:%S"))) << wxT("\n");


	extendedInfo->SetLabel(extInfo);

	wxString ip;
	buddy->getIp(ip);


	wxBitmap avatar = buddy->getAvatar();
	if(avatar.IsNull()){
		iUserInterface* ui = Impp::exemplar()->getModuleManager()->getUserInterfaceModule();
		if(ui != NULL){
			avatar = ui->getIcons()->getSpecialBitmap(BITMAP_NO_AVATAR);
		}
		Impp::exemplar()->requestAvatar(buddy);
	}
	avatarBitmap->SetBitmap(avatar);

	if(nickname.IsEmpty()){
		Impp::exemplar()->requestShortUserInfo(buddy);
	}

	mainPane->Layout();
	SetPosition(pos);
	Refresh();
}


void EntryToolTip::trigger(Buddy* buddy,const wxPoint & pos){
	if(curBuddy!=buddy){
		showBuddyInfo(buddy,pos);
	}
	if(!IsRunning()){
		Start(500,true);
	}
}


void EntryToolTip::hide(){
	Stop();
	Show(false);
}

void EntryToolTip::Notify(){
	Show(true);
}

Buddy* EntryToolTip::getCurBuddy(){
	return curBuddy;
}
void EntryToolTip::refreshInfo(){
	showBuddyInfo(curBuddy,curPos);
}

}
